package cn.zz.veggie.model.query;

import java.util.Objects;
import java.util.Optional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.zz.comm.core.types.query.CommQuery;
import cn.zz.comm.mybaits.types.BaseQuery;
import cn.zz.veggie.core.enums.biz.OrderStatus;
import cn.zz.veggie.model.domain.Order;
import cn.zz.veggie.model.sort.OrderSort;

/**
 * 订单表(Order)查询器
 *
 * @author zhongq
 * @since 2024-04-27 12:07:37
 */

public class OrderQuery extends CommQuery<Integer> implements BaseQuery<Order> {
	public Integer shopId;
	public String sn;
	public Integer status;
	public Integer delManId;
	public Long deliveryTimeStart;
	public Long deliveryTimeEnd;
	public OrderSort sort;

	public static OrderQuery ofSn(String sn) {
		final OrderQuery query = new OrderQuery();
		query.sn = sn;
		return query;
	}

	public static OrderQuery ofDelivery(Long deliveryTimeStart, Long deliveryTimeEnd) {
		return ofShopDelivery(null, deliveryTimeStart, deliveryTimeEnd);
	}

	public static OrderQuery ofShopDelivery(Integer shopId, Long deliveryTimeStart, Long deliveryTimeEnd) {
		final OrderQuery query = new OrderQuery();
		query.shopId = shopId;
		query.deliveryTimeStart = deliveryTimeStart;
		query.deliveryTimeEnd = deliveryTimeEnd;
		query.status = OrderStatus.active.status;
		return query;
	}

	@Override
	public void injectCondition(LambdaQueryWrapper<Order> queryWrapper) {
		queryWrapper.eq(Objects.nonNull(id), Order::getId, id);
		queryWrapper.in(Objects.nonNull(ids), Order::getId, ids);
		queryWrapper.eq(Objects.nonNull(shopId), Order::getShopId, shopId);
		queryWrapper.eq(Objects.nonNull(sn), Order::getSn, sn);
		queryWrapper.eq(Objects.nonNull(status), Order::getStatus, status);
		queryWrapper.eq(Objects.nonNull(delManId), Order::getDelManId, delManId);
		queryWrapper.ge(Objects.nonNull(deliveryTimeStart), Order::getDeliveryTime, deliveryTimeStart);
		queryWrapper.lt(Objects.nonNull(deliveryTimeEnd), Order::getDeliveryTime, deliveryTimeEnd);
	}

	@Override
	public void injectOrderBy(Page<Order> page) {
		// order by
		Optional.ofNullable(sort).map(OrderSort::toOrderItem).ifPresent(page::addOrder);
	}
}